import 'dart:async';

import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:imperius_insights/entity/department.dart';
import 'package:imperius_insights/entity/hr.dart';
import 'package:imperius_insights/events/event-bus.dart';
import 'package:imperius_insights/events/scope-changed-event.dart';
import 'package:imperius_insights/modules/scope/domain.dart' as Scope;
import 'package:imperius_insights/portal/state/mobx/store.dart';
import 'package:imperius_insights/routes/navigator.dart';
import 'package:imperius_insights/routes/paths.dart';
import 'package:mobx/mobx.dart';
import 'package:provider/provider.dart';

import 'routers.dart';
import 'widgets/menu/menu_data.dart';

part 'view_model.g.dart';

class ViewModel = _ViewModelInternal with _$ViewModel;

abstract class _ViewModelInternal with Store, StoreWithLifecycle {
  @observable
  List<Menu> menuItems;
  @observable
  Menu currentMenu;
  @observable
  Department department;
  @observable
  HumanResource user;

  StreamSubscription<ScopeChangedEvent> _scopeChangedSubscriber;

  @computed
  String get displayName {
    if (user == null) {
      return '全部人员';
    }
    return user.name ?? '莉亚';
  }

  @computed
  String get departmentName {
    return department?.displayName ?? '至高天';
  }

  @computed
  int get currentIndex {
    return menuItems.indexOf(currentMenu);
  }

  @override
  onContextAttached(BuildContext context) {
    super.onContextAttached(context);
    _scopeChangedSubscriber = defaultEventBus.on<ScopeChangedEvent>().listen(onScopeChanged);
    refresh();
    flowGuide();
  }

  onScopeChanged(ScopeChangedEvent event) async {
    refresh();
  }

  refresh() async {
    final domain = Provider.of<Scope.Domain>(context, listen: false);
    final departmentId = await domain.getDepartmentId();
    final humanId = await domain.getScopeHumanId();
    runInAction(() {
      department = domain.queryDepartment(departmentId: departmentId);
      user = domain.queryHumanInCache(humanId);
      this.currentMenu = menuItems[1];
    });
  }

  @override
  onContextDetached(BuildContext context) {
    super.onContextDetached(context);
    _scopeChangedSubscriber.cancel();
  }

  @action
  setUser(Department department, HumanResource user) {
    this.department = department;
    this.user = user;
  }

  @action
  onTapTab(int index) {
    currentMenu = menuItems[index];
  }

  _ViewModelInternal() {
    menuItems = ObservableList.of([
      Menu(title: '数据概览', icon: Icons.dashboard, routeCreator: dashboard),
      Menu(title: '项目管理', icon: Icons.view_list, routeCreator: projects),
      Menu(title: '工程质量', icon: Icons.insert_chart, routeCreator: dashboard),
      Menu(title: '人效分析', icon: Icons.watch_later, routeCreator: dashboard),
      Menu(title: '工具箱', icon: Icons.explore, routeCreator: toolbox),
      Menu(title: '程式设置', icon: Icons.settings_applications, routeCreator: dashboard),
      Menu(title: '关于', icon: Icons.info, routeCreator: dashboard)
    ]);
    currentMenu = menuItems[1];
  }

  flowGuide() async {
    final hasDepartmentId = await Provider.of<Scope.Domain>(context, listen: false).hasDepartmentId();
    if (hasDepartmentId) {
      return;
    }
    navigateTo(context, Paths.scopeDepartments, transition: TransitionType.inFromBottom);
  }

  onClickDisplayName() {
    navigateTo(context, Paths.scopeHumans, transition: TransitionType.inFromBottom);
  }

  onClickDepartmentName() {
    navigateTo(context, Paths.scopeDepartments, transition: TransitionType.inFromBottom);
  }
}